892B - Wrath - CodeForces Solution


greedy implementation two pointers *1200

Please click on ads to support us..

Python Code:

n = int(input())
l = list(map(int,input().split()))

killed = 0
last_killed = n

for i in range(n-1,-1,-1):
    cur_kill = max(0,i-l[i])
    if cur_kill >= last_killed:
        continue
    if i < last_killed:
        killed += i - cur_kill
    else:
        killed += last_killed - cur_kill
    if cur_kill == 0:
        break
    last_killed = min(last_killed,cur_kill)
    
print(n - killed)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
#define FAST()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
 
#define wTC()                         \
    int t;                            \
    cin >> t;                         \
    for (int i = 1; i <= t; i++)      \
    {                                 \
        cout << "Case " << i << ": "; \
        nasim();                      \
    }
#define TC()     \
    int t;       \
    cin >> t;    \
    while (t--)  \
    {            \
        nasim(); \
    }
#define nTC()    \
    int t = 1;   \
    while (t--)  \
    {            \
        nasim(); \
    }
    
#define endl "\n"
#define ll long long int
#define ull unsigned long long int
#define mod 1e9 + 7
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define getunique(v)                                  \
    {                                                 \
        sort(v.begin(), v.end());                     \
        v.erase(unique(v.begin(), v.end()), v.end()); \
    }

void nasim()
{
    ll n;
    cin>>n;
    ll arr[n+4];
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }    
    ll life = arr[n-1];
    ll cnt =0;
    for(ll i=n-2;i>=0;i--)
    {
        if(life!=0)
        {
            cnt++;
            life = max(arr[i],life-1);
        }
        else
        {
            life = max(arr[i],life-1);
        }
    }
    cout<<n-cnt<<endl;
}
int main()
{
    FAST()
    /*
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    */
 
    nTC();
 
    // TC();
 
    // wTC();
}


Comments

Submit
0 Comments
More Questions

162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted